package com.yanqu.road.server.manager.activity.xingyou.inter.grid;

import com.yanqu.road.entity.activity.youxing.ShanShuiGridParam;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiGridInfo;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.manager.activity.xingyou.DiceHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.GridHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.IXingYouGridHandler;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

/**
 * 银票格
 */
public class SliverGridHandler implements IXingYouGridHandler {

    @Override
    public void handle(XingYouShanShuiGridInfo gridInfo, int pos, int gridId, int endPos, boolean fly, DiceHandleResult result) {
        //是否停留
        boolean stop = pos == endPos;

        if((gridInfo.getTriggerType() == 1 && stop) || gridInfo.getTriggerType() == 2){

            GridHandleResult gridHandleResult = new GridHandleResult();

            XingYouShanShuiProto.ShanShuiEventInfo.Builder eventBuilder = XingYouShanShuiProto.ShanShuiEventInfo.newBuilder();
            eventBuilder.setPosition(pos);
            eventBuilder.setGridId(gridId);
            Property property = new Property();

            Property fixedReward = result.getActivity().getGridInfoFixedReward(result.getUserData(), gridInfo, stop);

            property.addProperty(fixedReward);
            eventBuilder.setFixedReward(PropertyHelper.parsePropertyToString(fixedReward));

            if(result.getUserData().isDrift()) {
                //漂泊地图
                property.addProperty(result.getConfig().getSHANSHUI_SILVER_COIN_ID(), gridInfo.getParam().get(0));
                eventBuilder.setBaseReward(PropertyHelper.parsePropertyToString(property));
            }else{
                //自家地图
                property.addProperty(result.getConfig().getSHANSHUI_SILVER_COIN_ID(), gridInfo.getParam().get(0));
                int roleBuffAddition = result.getActivity().getRoleBuffAddition(result.getUserData(), 4);
                //事件跳转的
                if(result.isEventGridJump()){
                    roleBuffAddition += result.getActivity().getRoleBuffAddition(result.getUserData(), 2);
                }
                eventBuilder.setBaseReward(PropertyHelper.parsePropertyToString(property));
                result.getActivity().calRewardBuff(result.getUserData(), property, false, roleBuffAddition);
            }

            eventBuilder.setAward(PropertyHelper.parsePropertyToString(property));
            gridHandleResult.setBuilder(eventBuilder);

            ShanShuiGridParam gridParam = new ShanShuiGridParam(gridId, pos, true, result.getUserData().isDrift());
            gridHandleResult.setGridParam(gridParam);

            result.getGridHandleResultList().add(gridHandleResult);

            if(stop) {
                result.getActivity().tryCatch(pos, result.getUserData(), result, gridHandleResult);
            }
        }
    }
}
